package com.example.wengwyue.patientapplication.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.hospital.common.util.LocalBroadcasts;


/**
 * 作者： wenyue   时间： 16/6/4.
 * 功能：基础activity 生命周期细分
 * 修改：
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected View mRootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int layoutId = provideLayoutResId();
        if (layoutId > 0) {
            mRootView = getLayoutInflater().inflate(layoutId, null);
            setContentView(mRootView);
        }

        View contentView = provideContentView();
        if (null != contentView) {
            mRootView = contentView;
            setContentView(contentView);
        }

        initView(savedInstanceState);
        initListener();
        initBroadcast();
        initData(savedInstanceState);
    }

    /**
     * 提供给BaseActivity，并在initView方法调用之前setContentView(int id)
     *
     * @return 如果不通过BaseActivity自动设置ContentView，则-1
     */
    protected abstract int provideLayoutResId();

    /**
     * 提供给BaseActivity，并在initView方法调用之前setContentView(View view)
     * <p/>
     * <br/><br/>
     * <p/>
     * 后于 {@link #provideLayoutResId()} 判断执行
     *
     * @return
     */
    protected View provideContentView() {
        return null;
    }

    /**
     * 步骤一：初始化View，比如findViewById等操作
     */
    protected abstract void initView(Bundle savedInstanceState);

    /**
     * 步骤二：初始化View的Listener，比如onClick等监听器
     */
    protected abstract void initListener();

    /**
     * 步骤三：初始化数据
     */
    protected abstract void initData(Bundle savedInstanceState);
    private BroadcastReceiver mPrReceiver;
    protected void initBroadcast() {
        String[] actions = provideBroadcastActions();
        if (null == actions || actions.length == 0) {
            return ;
        }
        registerLocalBroadcastReceiver(mPrReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                onReceiveBroadcast(intent.getAction(), intent);
            }
        }, actions);
    }
    protected String[] provideBroadcastActions() {
        return null;
    }

    /**
     * 根据{@link #provideBroadcastActions()}中提供的
     * @param action Intent中的action，方便调用
     * @param intent 广播Intent
     */
    protected void onReceiveBroadcast(String action, Intent intent) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterLocalReceiver(mPrReceiver);
    }

    protected void registerLocalBroadcastReceiver(BroadcastReceiver receiver, String... actions) {
        LocalBroadcasts.registerLocalReceiver(receiver, actions);
    }

    protected void registerLocalBroadcastReceiver(BroadcastReceiver receiver, IntentFilter...filters) {
        LocalBroadcasts.registerLocalReceiver(receiver, filters);
    }

    protected void unregisterLocalReceiver(BroadcastReceiver receiver) {
        LocalBroadcasts.unregisterLocalReceiver(receiver);
    }
}
